<template>
  <div style="height: 100%">
    <el-scrollbar style="height: 100%">
      <el-row v-show="showSearch" class="query_el_row-wrapper" style="margin: 0;padding-bottom: 18px;">
        <el-col :span="17">
          <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="queryDataList">
            <el-form-item label="图片检测状态" prop="predictStatus">
              <el-select v-model="state.queryForm.predictStatus" placeholder="请选择图片检测状态：待检测、健康、不健康">
                <el-option label="请选择">0</el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="上传时间" prop="createTime">
              <el-date-picker type="datetime" placeholder="请输入上传时间" v-model="state.queryForm.createTime"></el-date-picker>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="7">
          <div class="query_button_wrap">
            <el-button icon="search" type="primary" @click="queryDataList">
              查询
            </el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </div>

        </el-col>
      </el-row>


      <el-row class="add_el_row_wrapper" style="background: white;margin: 0;padding-bottom: 18px;padding-left: 20px">
        <el-col :span="15" class="add_el_button_el_col">
          <el-button plain :disabled="multiple" icon="Delete" type="primary"
                     @click="handleDelete(selectObjs)">
            删除
          </el-button>
        </el-col>
        <el-col :span="9" class="right_tool_el_col">
          <div style="margin-right: 10px"></div>
          <right-toolbar v-model:showSearch="showSearch" :export="'duorou_duorouKgbotcamFile_export'"
                         @exportExcel="exportExcel" class="ml10 mr20" style="float: right;"
                         @queryTable="queryDataList"></right-toolbar>
        </el-col>

      </el-row>

      <el-row v-show="showSearch" v-if="false">
        <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="queryDataList">
          <el-form-item label="图片检测状态" prop="predictStatus">
            <el-select v-model="state.queryForm.predictStatus" placeholder="请选择图片检测状态：待检测、健康、不健康">
              <el-option label="请选择">0</el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="上传时间" prop="createTime">
            <el-date-picker type="datetime" placeholder="请输入上传时间" v-model="state.queryForm.createTime"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button icon="search" type="primary" @click="queryDataList">
              查询
            </el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row v-if="false">
        <div class="mb8" style="width: 100%">
          <el-button plain :disabled="multiple" icon="Delete" type="primary"
                     @click="handleDelete(selectObjs)">
            删除
          </el-button>
          <right-toolbar v-model:showSearch="showSearch" :export="'duorou_duorouKgbotcamFile_export'"
                         @exportExcel="exportExcel" class="ml10 mr20" style="float: right;"
                         @queryTable="queryDataList"></right-toolbar>
        </div>
      </el-row>

      <el-row class="data_list_show_el_row_wrapper">
        <div style="width: 100%;height: 1px"></div>
        <el-table :data="state.dataList" v-loading="state.loading"
                  :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
                  @selection-change="selectionChangHandle"
                  @sort-change="sortChangeHandle">
          <el-table-column type="selection" width="40" align="center"/>
          <el-table-column type="index" label="#" width="60"/>
          <el-table-column prop="fileName" label="原始图片" width="130">
            <template #default="scope">
              <div style="width: 100%;cursor: pointer" @click="previewImg(scope,false)">
                <el-image :src="thisPageState.imgBaseUrl+scope.row.fileName" :fit="'cover'" style="width: 100px;height: 100px">

                </el-image>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="detectedFileName" label="检测图片"  width="130">
            <template #default="scope">
              <div style="width: 100%;cursor: pointer" @click="previewImg(scope,true)">
                <el-image :src="thisPageState.imgBaseUrl+scope.row.detectedFileName" :fit="'cover'" style="width: 100px;height: 100px">

                </el-image>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="detectedJson" label="初始检测数据" show-overflow-tooltip/>
          <el-table-column prop="predictStatus" label="检测结果" show-overflow-tooltip/>
          <el-table-column prop="bucketName" label="文件存储桶名称" show-overflow-tooltip/>
          <el-table-column prop="type" label="文件类型" show-overflow-tooltip/>
          <el-table-column prop="fileSize" label="文件大小" show-overflow-tooltip/>
          <el-table-column prop="createBy" label="创建人" show-overflow-tooltip/>
          <el-table-column prop="createTime" label="上传时间" show-overflow-tooltip/>
          <el-table-column label="操作" width="150">
            <template #default="scope">
              <el-button icon="delete" text type="primary"
                         @click="handleDelete([scope.row.id])">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>

      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination"/>
    </el-scrollbar>
  </div>

<!--  预览原始抓取图片-->
  <el-image-viewer v-if="thisPageState.showCapImg" :url-list="thisPageState.camCapimgList"
                   :initial-index="thisPageState.capImgViewIndex" @close="thisPageState.showCapImg=false">
  </el-image-viewer>

<!--  预览检测后的图片-->
  <el-image-viewer v-if="thisPageState.showCapImgDetected" :url-list="thisPageState.camCapDetectedList"
                   :initial-index="thisPageState.capImgDetectedViewIndex" @close="thisPageState.showCapImgDetected=false">

  </el-image-viewer>
</template>

<script setup lang="ts" name="systemDuorouKgbotcamFileDataCap">
import {BasicTableProps, useTable} from "/@/hooks/table";
import {fetchList, delObjs} from "/@/api/duorou/duorouKgbotcamFile";
import {useMessage, useMessageBox} from "/@/hooks/message";
import {useDict} from '/@/hooks/dict';
// import {parseTime} from "/@/utils/formatTime";
const { proxy } = getCurrentInstance();


/**
 * 定义组件属性 接收传递来的 大棚ID
 */
const props = defineProps({
  iotDevice: {
    type:Object,
    default: () => {},
  },
})


// 定义查询字典


// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {
    iotDeviceId: props.iotDevice.id,
  },
  pageList: fetchList,
  handleDataListcallBack: getPreviewImagListByDataTable
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
  tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  queryDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/duorou/duorouKgbotcamFile/export', Object.assign(state.queryForm, {ids: selectObjs}), 'duorouKgbotcamFile.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({id}) => id);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm('此操作将永久删除');
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    queryDataList()
    useMessage().success('删除成功');
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};


/**
 * 重写查询方法
 */
function queryDataList(){
  state.queryForm.createTime=proxy.parseTime(state.queryForm.createTime)
  getDataList(getPreviewImagListByDataTable)
}


/**
 * 自己定义的页面state
 */
const thisPageState=reactive({
  camCapimgList:[],
  showCapImg:false,
  capImgViewIndex:0,

  camCapDetectedList:[],
  showCapImgDetected:false,
  capImgDetectedViewIndex:0,

  imgBaseUrl:"/api"
})

/**
 * 获取后端地址
 */
thisPageState.imgBaseUrl=import.meta.env.VITE_API_URL

/**
 * 获取数据图片的预览列表
 */
function getPreviewImagListByDataTable(){
  let list=[]
  list = state.dataList?.map(v => thisPageState.imgBaseUrl+v.fileName) ?? [];
  thisPageState.camCapimgList=list
  list = state.dataList?.map(v => thisPageState.imgBaseUrl+v.detectedFileName) ?? [];
  thisPageState.camCapDetectedList=list
}


/**
 * 预览图片
 * @param scope
 * @param isDetected
 */
function previewImg(scope,isDetected){
  console.log(scope)
  if(isDetected){
    thisPageState.showCapImgDetected=true
    thisPageState.capImgDetectedViewIndex=scope.$index
  }else{
    thisPageState.showCapImg=true
    thisPageState.capImgViewIndex=scope.$index
  }
}
</script>

<style scoped lang="scss">

</style>